//
//  UIView+Gesture.m
//  高仿暴走斗图
//
//  Created by huangsongyao on 16/3/7.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "UIView+Gesture.h"
#import "UIGestureRecognizer+RACSignalSupport.h"

@implementation UIView (Gesture)

+ (id)addGestureRecognizerWithGesturesType:(kGesturesType)gesturesType action:(SEL)action delegate:(id<UIGestureRecognizerDelegate>)delegate {
    
    id gesture;
    switch (gesturesType) {
        case kGesturesTypeTapGestureRecognizer:
        {
            //单击手势，响应手指和点击次数都为1
            gesture = [UIView addTapGestureRecognizerWithAction:action delegate:delegate numberOfTouchesRequired:1 numberOfTapsRequired:1];
        }
            break;
        case kGesturesTypePinchGestureRecognizer:
        {
            //放缩手势，相对触点为10px
            gesture = [UIView addPinchGestureRecognizerWithAction:action delegate:delegate scale:10];
        }
            break;
        case kGesturesTypeRotationGestureRecognizer:
        {
            //旋转手指，角度为50
            gesture = [UIView addRotationGestureRecognizerWithAction:action delegate:delegate rotation:50];
        }
            break;
        case kGesturesTypeSwipeGestureRecognizer:
        {
            //滑动手势，响应手指数1，方向默认向右
            gesture = [UIView addSwipeGestureRecognizerWithAction:action delegate:delegate numberOfTouchesRequired:1 direction:UISwipeGestureRecognizerDirectionRight];
        }
            break;
        case kGesturesTypePanGestureRecognizer:
        {
            //拖动手势，最大最小响应手势都为1
            gesture = [UIView addPanGestureRecognizerWithAction:action delegate:delegate minimumNumberOfTouches:1 maximumNumberOfTouches:1];
        }
            break;
        case kGesturesTypeLongPressGestureRecognizer:
        {
            //长按手势，响应手指数为1，响应时间为0.5f，作用域为10px扩散
            gesture = [UIView addLongPressGestureRecognizerWithAction:action delegate:delegate numberOfTapsRequired:0 numberOfTouchesRequired:1 minimumPressDuration:0.5f allowableMovement:10];
        }
            break;
        default:
            break;
    }
    
    return gesture;
}


+ (UITapGestureRecognizer *)addTapGestureRecognizerWithAction:(SEL)action delegate:(id<UIGestureRecognizerDelegate>)delegate numberOfTouchesRequired:(NSInteger)numberOfTouchesRequired numberOfTapsRequired:(NSInteger)numberOfTapsRequired {
    
    UITapGestureRecognizer *tapGestrue = [[UITapGestureRecognizer alloc] initWithTarget:delegate action:action];
    tapGestrue.numberOfTouchesRequired = numberOfTouchesRequired; //点击手势的手指个数
    tapGestrue.numberOfTapsRequired = numberOfTapsRequired; //tap次数
    
    return tapGestrue;
}

+ (UIPinchGestureRecognizer *)addPinchGestureRecognizerWithAction:(SEL)action delegate:(id<UIGestureRecognizerDelegate>)delegate scale:(CGFloat)scale {
    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:delegate action:action];
    pinchGesture.scale = scale;//相对于屏幕坐标的接触点
    
    return pinchGesture;
}


+ (UIRotationGestureRecognizer *)addRotationGestureRecognizerWithAction:(SEL)action delegate:(id<UIGestureRecognizerDelegate>)delegate rotation:(CGFloat)rotation {
    
    UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:delegate action:action];
    rotationGesture.rotation = rotation;//旋转角度
    
    return rotationGesture;
}


+ (UISwipeGestureRecognizer *)addSwipeGestureRecognizerWithAction:(SEL)action delegate:(id<UIGestureRecognizerDelegate>)delegate numberOfTouchesRequired:(CGFloat)numberOfTouchesRequired direction:(UISwipeGestureRecognizerDirection)direction {
    
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:delegate action:action];
    swipeGesture.numberOfTouchesRequired = numberOfTouchesRequired;//滑动手指数目
    swipeGesture.direction = direction;//滑动手势方向
    
    return swipeGesture;
}


+ (UIPanGestureRecognizer *)addPanGestureRecognizerWithAction:(SEL)action delegate:(id<UIGestureRecognizerDelegate>)delegate minimumNumberOfTouches:(NSInteger)minimumNumberOfTouches maximumNumberOfTouches:(NSInteger)maximumNumberOfTouches {
    
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:delegate action:action];
    
    panGesture.minimumNumberOfTouches = minimumNumberOfTouches;//最少手指数目
    panGesture.maximumNumberOfTouches = maximumNumberOfTouches;//最多手指数目
    panGesture.delaysTouchesEnded = YES;
    
    return panGesture;
}


+ (UILongPressGestureRecognizer *)addLongPressGestureRecognizerWithAction:(SEL)action delegate:(id<UIGestureRecognizerDelegate>)delegate numberOfTapsRequired:(NSInteger)numberOfTapsRequired numberOfTouchesRequired:(NSInteger)numberOfTouchesRequired minimumPressDuration:(CFTimeInterval)minimumPressDuration allowableMovement:(CGFloat)allowableMovement {
    
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:action];
    longPressGesture.numberOfTapsRequired = numberOfTapsRequired;//长按次数
    longPressGesture.numberOfTouchesRequired = numberOfTouchesRequired;//长按手指数目
    longPressGesture.minimumPressDuration = minimumPressDuration;
    longPressGesture.allowableMovement = allowableMovement;
    longPressGesture.delegate = delegate;
    
    return longPressGesture;
}

+ (id)addGestureRecognizerWithAction:(SEL)action delegate:(id<UIGestureRecognizerDelegate>)delegate gestureRecognizerClass:(Class)gestureRecognizerClass {
    
    UIGestureRecognizer *gesture = [[gestureRecognizerClass alloc] initWithTarget:delegate action:action];
    return gesture;
}


+ (UITapGestureRecognizer *)rad_addTapGestureRecognizerWithDelegate:(id<UIGestureRecognizerDelegate>)delegate numberOfTouchesRequired:(NSInteger)numberOfTouchesRequired numberOfTapsRequired:(NSInteger)numberOfTapsRequired selectBlock:(void(^)(id x))block {
    
    UITapGestureRecognizer *tapGestrue = [UIView addTapGestureRecognizerWithAction:nil
                                                                          delegate:delegate
                                                           numberOfTouchesRequired:numberOfTouchesRequired
                                                              numberOfTapsRequired:numberOfTapsRequired];
    
    [[[tapGestrue rac_gestureSignal] deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id x) {
        if (block) {
            block(x);
        }
    }];
    return tapGestrue;
}

+ (UISwipeGestureRecognizer *)rac_addSwipeGestureRecognizerWithDelegate:(id<UIGestureRecognizerDelegate>)delegate numberOfTouchesRequired:(CGFloat)numberOfTouchesRequired direction:(UISwipeGestureRecognizerDirection)direction swipeBlock:(void(^)(id x))block
{
    UISwipeGestureRecognizer *swipeGesture = [UIView addSwipeGestureRecognizerWithAction:nil
                                                                                delegate:delegate
                                                                 numberOfTouchesRequired:numberOfTouchesRequired
                                                                               direction:direction];
    [[[swipeGesture rac_gestureSignal] deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id x) {
        if (block) {
            block(x);
        }
    }];
    return swipeGesture;
}

+ (UIPanGestureRecognizer *)rac_addPanGestureRecognizerWithDelegate:(id<UIGestureRecognizerDelegate>)delegate minimumNumberOfTouches:(NSInteger)minimumNumberOfTouches maximumNumberOfTouches:(NSInteger)maximumNumberOfTouches panBlock:(void(^)(id x))block
{
    UIPanGestureRecognizer *pan = [UIView addPanGestureRecognizerWithAction:nil
                                                                   delegate:delegate
                                                     minimumNumberOfTouches:minimumNumberOfTouches
                                                     maximumNumberOfTouches:maximumNumberOfTouches];
    [[[pan rac_gestureSignal] deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id x) {
        if (block) {
            block(x);
        }
    }];
    return pan;
}

@end
